<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/18-11:13
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\addons\util;


    use app\admin\controller\AdminBase;
    use app\addons\model\Addons as Addons_model;

    /**
     * Class Adminaddon
     * @package app\addons\util
     */
    class Adminaddon extends AdminBase
    {

        /**
         * @var null //插件标识
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/18-11:15
         */
        public $addonName = null;

        /**
         * @var null  //插件基本信息
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/18-11:15
         */
        protected $addonInfo = null;

        /**
         * @var null  //插件路径
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/18-11:15
         */
        protected $addonPath = null;

        /**
         * @throws \think\Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/18
         * @name: initialize
         * @describe:
         */
        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub
            parent::initialize();
            $this->addonName = \think\Loader::parseName($this->request->controller());
            $this->Addons_model = new Addons_model;
            $this->addonInfo = $this->Addons_model->where(['name' => $this->addonName])->find();
            if (empty($this->addonInfo)) {
                $this->error('该插件没有安装！');
            }
            if (!$this->addonInfo['status']) {
                $this->error('该插件已被禁用！');
            }
            $this->addonPath = ADDON_PATH.$this->addonName.DIRECTORY_SEPARATOR;
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/18
         * @name: getAddonConfig
         * @describe:获取插件配置
         */
        final public function getAddonConfig()
        {
            $config = $this->addonInfo['config'];
            if ($config) {
                $config = json_decode($config, true);
            }

            return $config;
        }



        /**
         * @param string $template
         * @param array $vars
         * @param array $config
         * @return mixed|string
         * @throws \Exception
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/18
         * @name: fetch
         * @describe:加载模板输出
         */
        protected function fetch($template = '', $vars = [], $config = [])
        {
            return $this->view->fetch($this->parseAddonTemplateFile($template, $this->addonPath), $vars, $config);
        }



        /**
         * @param $template
         * @param $addonPath
         * @return string
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/18
         * @name: parseAddonTemplateFile
         * @describe:自动定位模板文件
         */
        private function parseAddonTemplateFile($template, $addonPath)
        {
            if (0 !== strpos($template, '/')) {
                $template = str_replace(['/', ':'], DIRECTORY_SEPARATOR, $template);
                if ($this->addonName) {
                    if ('' == $template) {
                        // 如果模板文件名为空 按照默认规则定位
                        $template = str_replace(
                                '.',
                                DIRECTORY_SEPARATOR,
                                $this->addonName
                            ).DIRECTORY_SEPARATOR.$this->request->action();
                    } elseif (false === strpos($template, DIRECTORY_SEPARATOR)) {
                        $template = str_replace(
                                '.',
                                DIRECTORY_SEPARATOR,
                                $this->addonName
                            ).DIRECTORY_SEPARATOR.$template;
                    }
                }
            } else {
                $template = str_replace(['/', ':'], DIRECTORY_SEPARATOR, substr($template, 1));
            }

            return $addonPath.'view'.DIRECTORY_SEPARATOR.ltrim($template, '/').'.html';

        }

    }